
using System;
using System.Linq;
using Csla;
using Csla.Data;

namespace HousingInfo.Library
{ 
	public partial class TextHouseBOList : Csla.BusinessListBase<TextHouseBOList, TextHouseBO>
	{

		#region Authorization Rules

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of TextHouseBOList
			//AuthorizationRules.AllowCreate(typeof(TextHouseBOList), "TextHouseBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(TextHouseBOList), "TextHouseBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(TextHouseBOList), "TextHouseBOListDeleteGroup"));
		}
		#endregion //Authorization Rules

        #region Trang Duyet tin moi
        #region Ham lay cac tin chua duyet - Quang cao - Mien phi - Moi sua - Moi Gia han
        public static TextHouseBOList GetAllTextHouseNew(bool isactive)
        {
            return DataPortal.Fetch<TextHouseBOList>(new FilterCriteria_TextHouseNew(isactive));
        }
        #endregion
        #region Khai Bao ham
        private class FilterCriteria_TextHouseNew
        {
            public bool active;
            public FilterCriteria_TextHouseNew(bool acti)
            {
                this.active = acti;
            }
        }
        #endregion
        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteria_TextHouseNew param)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (param.active == true)
                {
                    this.AddRange(
                        from row in mgr.DataContext.TextHouses
                        where row.IsActive == true
                        orderby row.CreatedDate descending
                        select TextHouseBO.GetTextHouseBO(row.TextHouseID)
                    );
                }
                else
                {
                    this.AddRange(
                        from row in mgr.DataContext.TextHouses
                        where row.IsActive == false
                        orderby row.CreatedDate descending
                        select TextHouseBO.GetTextHouseBO(row.TextHouseID)
                    );
                }

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion
        #endregion

        #region Trang Van Ban - User - Tat Ca - Xem Nhieu - Download - Moi Nhat
        #region Ham
        public static TextHouseBOList GetTextHouse_ByOption(string typesearch, string keyword, int sotrang, int soluong)
        {
            return DataPortal.Fetch<TextHouseBOList>(new FilterCriteria_ByOption(typesearch, keyword, sotrang, soluong));
        }
        #endregion
        #region Khai Bao ham
        private class FilterCriteria_ByOption
        {
            public string kieutim;
            public string tukhoa;
            public int sotrang;
            public int soluong;
            public FilterCriteria_ByOption(string kieut, string tuk, int sot, int sol)
            {
                this.kieutim = kieut;
                this.tukhoa = tuk;
                this.sotrang = sot;
                this.soluong = sol; 
            }
        }
        #endregion
        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteria_ByOption param)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                var result = from row in mgr.DataContext.TextHouses
                             where row.IsActive == true
                             select row;
                if (param.tukhoa == null)
                {
                    if (param.kieutim == "moinhat")
                    {
                        result = from row in result
                                 orderby row.CreatedDate descending
                                 select row;
                    }
                    else if (param.kieutim == "xemnhieu")
                    {
                        result = from row in result
                                 orderby row.ViewCount descending
                                 select row;
                    }
                    else if (param.kieutim == "download")
                    {
                        result = from row in result
                                 orderby row.DownloadCount descending
                                 select row;
                    }
                }
                else
                {
                    result = from row in result
                             where row.Title.Contains(param.tukhoa)
                             select row;
                }

                if (param.soluong == -1 && param.sotrang == -1)
                {
                    this.AddRange(
                        from row in result
                        select TextHouseBO.GetTextHouseBO(row.TextHouseID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in result
                        select TextHouseBO.GetTextHouseBO(row.TextHouseID)).Skip((param.sotrang - 1)*param.soluong).Take(param.soluong)
                    );
                }

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion
        #endregion

        #region Lay mot so tin van ban khac
        #region Ham lay cac tin chua duyet - Quang cao - Mien phi - Moi sua - Moi Gia han
        public static TextHouseBOList GetOrtherTextHouse(int idda_co, int soluong)
        {
            return DataPortal.Fetch<TextHouseBOList>(new FilterCriteria_OrtherTextHouse(idda_co, soluong));
        }
        #endregion
        #region Khai Bao ham
        private class FilterCriteria_OrtherTextHouse
        {
            public int id_daco;
            public int soluong;
            public FilterCriteria_OrtherTextHouse(int dc, int sl)
            {
                this.id_daco = dc;
                this.soluong = sl;
            }
        }
        #endregion
        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteria_OrtherTextHouse param)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                this.AddRange(
                    (from row in mgr.DataContext.TextHouses
                    where row.IsActive == true && row.TextHouseID != param.id_daco
                    orderby row.CreatedDate descending
                    select TextHouseBO.GetTextHouseBO(row.TextHouseID)).Take(param.soluong)
                );

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion
        #endregion


        #region Van ban dang dang - cho duyet cua 1 users
        #region Ham 
        public static TextHouseBOList GetTextHouse_ByUser(string username, string kieu)
        {
            return DataPortal.Fetch<TextHouseBOList>(new FilterCriteria_ByUser(username, kieu));
        }
        #endregion
        #region Khai Bao ham
        private class FilterCriteria_ByUser
        {
            public string username;
            public string kieu;
            public FilterCriteria_ByUser(string user, string ki)
            {
                this.username = user;
                this.kieu = ki;
            }
        }
        #endregion
        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteria_ByUser param)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (param.kieu == "tatca")
                {
                    this.AddRange(
                        from row in mgr.DataContext.TextHouses
                         where row.UserName == param.username
                         orderby row.CreatedDate descending
                         select TextHouseBO.GetTextHouseBO(row.TextHouseID)
                    );
                }
                else if (param.kieu == "dangdang")
                {
                    this.AddRange(
                        from row in mgr.DataContext.TextHouses
                        where row.UserName == param.username && row.IsActive == true
                        orderby row.CreatedDate descending
                        select TextHouseBO.GetTextHouseBO(row.TextHouseID)
                    );
                }
                else
                {
                    this.AddRange(
                        from row in mgr.DataContext.TextHouses
                        where row.UserName == param.username && row.IsActive == false
                        orderby row.CreatedDate descending
                        select TextHouseBO.GetTextHouseBO(row.TextHouseID)
                    );
                }

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion
        #endregion

    }
}
